/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hhf.flex.ascreator;

import com.sun.mirror.apt.AnnotationProcessor;
import com.sun.mirror.apt.AnnotationProcessorEnvironment;
import com.sun.mirror.apt.AnnotationProcessorFactory;
import com.sun.mirror.declaration.AnnotationTypeDeclaration;
import hhf.flex.annotations.FlexDestination;
import hhf.flex.annotations.entities.FlexObject;
import hhf.flex.annotations.entities.FlexSeeAlso;
import java.util.Collection;
import java.util.Set;
import java.util.Arrays;
import java.util.Collections;
import java.util.Collections.*;
import java.util.Map;

/**
 *
 * @author administrateur
 */
public class Factory implements AnnotationProcessorFactory {

	// Taiter les annotations du package
	private Collection<String> supportedAnnotations = Collections.unmodifiableCollection(Arrays.asList(FlexDestination.class.getName(), FlexSeeAlso.class.getName(), FlexObject.class.getName()));
	// Options (voir la doc pour + d'info)
	private Collection<String> supportedOptions = Collections.emptySet();

	@Override
	public final Collection<String> supportedOptions() {
		return supportedOptions;
	}

	@Override
	public final Collection<String> supportedAnnotationTypes() {
		return supportedAnnotations;
	}

	// Récupérer le processeur d'annotation
	@Override
	public final AnnotationProcessor getProcessorFor(final Set<AnnotationTypeDeclaration> atds, final AnnotationProcessorEnvironment env) {
		Map<String, String> map = env.getOptions();
		String errorsPath = null;
		for (String m : map.keySet()) {
			System.out.println("key " + m);
			if (m.startsWith("-AasErrorFilePath")) {
				errorsPath = extractValueFromAPTOption(m, "-AasErrorFilePath");
				break;
			}
		}
		String path = map.get("-d");
		String target = map.get("-extdirs");
		return new Processor(env, path, target, errorsPath);
	}

	private String extractValueFromAPTOption(String option, String key) {
		return option.replaceAll(key + "=", "");
	}
}
